public
private, protected ์ ์ฌ์ฉํ์ง ์์ผ๋ฏ๋ก ๋ณธ ๊ฐ์์์๋ public ์์์ ์ด์
Member functions of a derived class cannot directly access private members of the base class.
Base class | Derived classes |
---|---|
Student | GraduateStudent, UndergraduateStudent |
Shape | Circle, Triangle, Rectangle, Sphere, Cube |
Loan | CarLoan, HomeImprovementLoan, MortgageLoad |
Employee | Faculty, Staff |
Account | CheckingAccount, SavingAccount |
Class TwoDimensionalShape : public Shape
::
) ๋ฅผ ์ด์ฉํ์ฌ ์ ๊ทผํ ์ ์๋ค.
shape::area
#ifndef COMMISSION_H
#define COMMISSION_H
#include <string>
using std::string;
class CommissionEmployee
{
public:
CommissionEmployee(const string &, const string &, const string &, double = 0.0, double = 0.0);
void setFirstName(const string &);
string getFirstName() const;
void setLastName(const string &);
string getLastName() const;
void setSocialSecurityNumber(const string &);
string getSocialSecurityNumber() const;
void setGrossSales(double);
double getGrossSales() const;
void setCommissionRate(double);
double getCommissionRate() const;
double earnings() const;
void print() const;
private:
string firstName;
string lastName;
string socialSecurityNumber;
double grossSales;
double commissionRate;
};
#endif
#include <iostream>
using std::cout;
#include "CommissionEmployee.h"
CommissionEmployee::CommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate)
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
setGrossSales(sales);
setCommissionRate(rate);
}
void CommissionEmployee::setFirstName(const string &first)
{
firstName = first;
}
string CommissionEmployee::getFirstName() const
{
return firstName;
}
void CommissionEmployee::setLastName(const string &last)
{
lastName = last;
}
string CommissionEmployee::getLastName() const
{
return lastName;
}
void CommissionEmployee::setSocialSecurityNumber(const string &ssn)
{
socialSecurityNumber = ssn;
}
string CommissionEmployee::getSocialSecurityNumber() const
{
return socialSecurityNumber;
}
void CommissionEmployee::setGrossSales(double sales)
{
grossSales = (sales < 0.0) ? 0.0 : sales;
}
double CommissionEmployee::getGrossSales() const
{
return grossSales;
}
void CommissionEmployee::setCommissionRate(double rate)
{
commissionRate = (rate > 0.0 && rate < 1.0) ? rate : 0.0;
}
double CommissionEmployee::getCommissionRate() const
{
return commissionRate;
}
double CommissionEmployee::earnings() const
{
return commissionRate * grossSales;
}
void CommissionEmployee::print() const
{
cout << "commission employee: " << firstName << ' ' << lastName
<< "\nsocial security number: " << socialSecurityNumber
<< "\ngross sales: " << grossSales
<< "\ncommission rate: " << commissionRate;
}
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
#include "CommissionEmployee.h"
int main()
{
CommissionEmployee employee(
"Sue", "Jones", "222-22-2222", 10000, .06);
cout << fixed << setprecision(2);
cout << "Employee information obtained by get functions: \n"
<< "\nFirst name is " << employee.getFirstName()
<< "\nLast name is " << employee.getLastName()
<< "\nSocial security number is " << employee.getSocialSecurityNumber()
<< "\nGross sales is " << employee.getGrossSales()
<< "\nCommission rate is " << employee.getCommissionRate() << endl;
employee.setGrossSales(8000);
employee.setCommissionRate(.1);
cout << "\nUpdate employee information output: " << endl;
employee.print();
cout << "\n\nEmployee's earnings: $" << employee.earnings() << endl;
return 0;
}
#ifndef BASEPLUS_H
#define BASEPLUS_H
#include <string>
using std::string;
#include "CommissionEmployee.h"
class BasePlusCommissionEmployee : public CommissionEmployee
{
public:
BasePlusCommissionEmployee(const string &, const string &, const string &,
double = 0.0, double = 0.0, double = 0.0);
void setBaseSalary(double);
double getBaseSalary() const;
double earnings() const;
void print() const;
private:
double baseSalary;
};
#endif
#include <iostream>
using std::cout;
#include "BasePlusCommissionEmployee.h"
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary)
: CommissionEmployee(first, last, ssn, sales, rate)
{
setBaseSalary(salary);
}
void BasePlusCommissionEmployee::setBaseSalary(double salary)
{
baseSalary = (salary < 0.0) ? 0.0 : salary;
}
double BasePlusCommissionEmployee::getBaseSalary() const
{
return baseSalary;
}
double BasePlusCommissionEmployee::earnings() const
{
return baseSalary + (commissionRate * grossSales);
}
void BasePlusCommissionEmployee::print() const
{
cout << "base-salaried commission emplyee: " << firstName << ' '
<< lastName << "\nsocial security number: " << socialSecurityNumber
<< "\ngross sales: " << grossSales
<< "\ncommission rate: " << commissionRate
<< "\nbase salary: " << baseSalary;
}
์์์ ์ ๋์์ผ๋, Base class์ Private ๋ฉค๋ฒ์ ์ ๊ทผํ๋ ๋ฐฉ์์์ ์ปดํ์ผ ์ค๋ฅ ๋ฐ์
#ifndef COMMISSION_H
#define COMMISSION_H
#include <string>
using std::string;
class CommissionEmployee
{
public:
CommissionEmployee(const string &, const string &, const string &, double = 0.0, double = 0.0);
void setFirstName(const string &);
string getFirstName() const;
void setLastName(const string &);
string getLastName() const;
void setSocialSecurityNumber(const string &);
string getSocialSecurityNumber() const;
void setGrossSales(double);
double getGrossSales() const;
void setCommissionRate(double);
double getCommissionRate() const;
double earnings() const;
void print() const;
protected:
string firstName;
string lastName;
string socialSecurityNumber;
double grossSales;
double commissionRate;
};
#endif
#ifndef BASEPLUS_H
#define BASEPLUS_H
#include <string>
using std::string;
#include "CommissionEmployee.h"
class BasePlusCommissionEmployee : public CommissionEmployee
{
public:
BasePlusCommissionEmployee(const string &, const string &, const string &,
double = 0.0, double = 0.0, double = 0.0);
void setBaseSalary(double);
double getBaseSalary() const;
double earnings() const;
void print() const;
private:
double baseSalary;
};
#endif
#include <iostream>
using std::cout;
#include "BasePlusCommissionEmployee.h"
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary)
// explicitly call base-class constructor
: CommissionEmployee(first, last, ssn, sales, rate)
{
setBaseSalary(salary);
}
void BasePlusCommissionEmployee::setBaseSalary(double salary)
{
baseSalary = (salary < 0.0) ? 0.0 : salary;
}
double BasePlusCommissionEmployee::getBaseSalary() const
{
return baseSalary;
}
double BasePlusCommissionEmployee::earnings() const
{
// can access protected data of base class
return baseSalary + (commissionRate * grossSales);
}
void BasePlusCommissionEmployee::print() const
{
// can access protected data of base class
cout << "base-salaried commission emplyee: " << fFirstName << ' '
<< lastName << "\nsocial security number: " << socialSecurityNumber
<< "\ngross sales: " << grossSales
<< "\ncommission rate: " << commissionRate
<< "\nbase salary: " << baseSalary;
}
์ฅ์
set | get ํธ์ถํ๋๋ฐ ์๋ชจ๋๋ ์ค๋ฒํค๋๋ฅผ ํผํ ์ ์๋ค. |
๋จ์
์ ํจ์ฒดํฌ ๋ถ๊ฐ โ set | get ํจ์๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ฏ๋ก ํ์ ํด๋์ค์ ๋น์ ์์ ์ธ ๊ฐ์ด ํ ๋น๋ ์ ์๋ค. |
์ํํธ์จ์ด ๊ณตํ ๊ธฐ๋ฒ์ ๊ถ๊ณ ๋ฅผ ๋ฐ๋ฆ
๊ธฐ๋ณธ ํด๋์ค์ด๋ฆ::ํจ์ ์ด๋ฆ
๊ณผ ๊ฐ์ด ํธ์ถํด์ผ ํจ#include <iostream>
using std::cout;
#include "CommissionEmployee.h"
// constructor
CommissionEmployee::CommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate)
: firstName(first), lastName(last), socialSecurityNumber(ssn)
{
setGrossSales(sales);
setCommissionRate(rate);
}
void CommissionEmployee::setFirstName(const string &first)
{
firstName = first;
}
string CommissionEmployee::getFirstName() const
{
return firstName;
}
void CommissionEmployee::setLastName(const string &last)
{
lastName = last;
}
string CommissionEmployee::getLastName() const
{
return lastName;
}
void CommissionEmployee::setSocialSecurityNumber(const string &ssn)
{
socialSecurityNumber = ssn;
}
string CommissionEmployee::getSocialSecurityNumber() const
{
return socialSecurityNumber;
}
void CommissionEmployee::setGrossSales(double sales)
{
grossSales = (sales < 0.0) ? 0.0 : sales;
}
double CommissionEmployee::getGrossSales() const
{
return grossSales;
}
void CommissionEmployee::setCommissionRate(double rate)
{
commissionRate = (rate > 0.0 && rate < 1.0) ? rate : 0.0;
}
double CommissionEmployee::getCommissionRate() const
{
return commissionRate;
}
double CommissionEmployee::earnings() const
{
return getCommissionRate() * getGrossSales();
}
void CommissionEmployee::print() const
{
cout << "commission employee: " << getFirstName() << ' ' << getLastName()
<< "\nsocial security number: " << getSocialSecurityNumber()
<< "\ngross sales: " << getGrossSales()
<< "\ncommission rate: " << getCommissionRate();
}
#include <iostream>
using std::cout;
#include "BasePlusCommissionEmployee.h"
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary)
// explicitly call base-class constructor
: CommissionEmployee(first, last, ssn, sales, rate)
{
setBaseSalary(salary);
}
void BasePlusCommissionEmployee::setBaseSalary(double salary)
{
baseSalary = (salary < 0.0) ? 0.0 : salary;
}
double BasePlusCommissionEmployee::getBaseSalary() const
{
return baseSalary;
}
double BasePlusCommissionEmployee::earnings() const
{
// can access protected data of base class
return getBaseSalary() + CommissionEmployee::earnings();
}
void BasePlusCommissionEmployee::print() const
{
// can access protected data of base class
cout << "base-salaried ";
// invoke CommissionEmployee's print function
CommissionEmployee::print();
cout << "\nbase salary: " << getBaseSalary();
}
#include <iostream>
#include <iomanip>
using namespace std;
#include "BasePlusCommissionEmployee.h"
int main()
{
// instantiate BasePlusCommissionEmployee object
BasePlusCommissionEmployee
employee("Bob", "Lewis", "333-33-3333", 5000, .04, 300);
cout << fixed << setprecision(2);
cout << "Employee information obtained by get function: \n"
<< "\nFirst name is " << employee.getFirstName()
<< "\nLast name is " << employee.getLastName()
<< "\nsocial security number is " << employee.getSocialSecurityNumber()
<< "\nGross sales is " << employee.getGrossSales()
<< "\ncommission rate is " << employee.getCommissionRate()
<< "\nBase salary is " << employee.getBaseSalary() << endl;
employee.setGrossSales(1000);
// employee.setGrossSales(8000);
// employee.setCommissionRate(.1);
cout << "\nUpdated employee information ouput bt print function: \n"
<< endl;
employee.print();
cout << "\n\nEmployee's earning: $" << employee.earnings() << endl;
return 0;
}
#include <iostream>
using std::cout;
#include "CommissionEmployee.h"
// constructor
CommissionEmployee::CommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate)
: firstName(first), lastName(last), socialSecurityNumber(ssn)
{
setGrossSales(sales);
setCommissionRate(rate);
cout << "CommissionEmployee constuctor: " << endl;
print();
cout << "\n\n";
}
// destructor
CommissionEmployee::~CommissionEmployee()
{
cout << "CommissionEmployee desturctor: " << endl;
print();
cout << "\n\n";
}
void CommissionEmployee::setFirstName(const string &first)
{
firstName = first;
}
string CommissionEmployee::getFirstName() const
{
return firstName;
}
void CommissionEmployee::setLastName(const string &last)
{
lastName = last;
}
string CommissionEmployee::getLastName() const
{
return lastName;
}
void CommissionEmployee::setSocialSecurityNumber(const string &ssn)
{
socialSecurityNumber = ssn;
}
string CommissionEmployee::getSocialSecurityNumber() const
{
return socialSecurityNumber;
}
void CommissionEmployee::setGrossSales(double sales)
{
grossSales = (sales < 0.0) ? 0.0 : sales;
}
double CommissionEmployee::getGrossSales() const
{
return grossSales;
}
void CommissionEmployee::setCommissionRate(double rate)
{
commissionRate = (rate > 0.0 && rate < 1.0) ? rate : 0.0;
}
double CommissionEmployee::getCommissionRate() const
{
return commissionRate;
}
double CommissionEmployee::earnings() const
{
return getCommissionRate() * getGrossSales();
}
void CommissionEmployee::print() const
{
cout << "commission employee: " << getFirstName() << ' ' << getLastName()
<< "\nsocial security number: " << getSocialSecurityNumber()
<< "\ngross sales: " << getGrossSales()
<< "\ncommission rate: " << getCommissionRate();
}
#include <iostream>
using std::cout;
#include "BasePlusCommissionEmployee.h"
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary)
// explicitly call base-class constructor
: CommissionEmployee(first, last, ssn, sales, rate)
{
setBaseSalary(salary);
cout << "BasePlusCommissionEmployee constructor: " << endl;
print();
cout << "\n\n";
}
// destructor
BasePlusCommissionEmployee::~BasePlusCommissionEmployee()
{
cout << "BasePlusCommissionEmployee destructor: " << endl;
print();
cout << "\n\n";
}
void BasePlusCommissionEmployee::setBaseSalary(double salary)
{
baseSalary = (salary < 0.0) ? 0.0 : salary;
}
double BasePlusCommissionEmployee::getBaseSalary() const
{
return baseSalary;
}
double BasePlusCommissionEmployee::earnings() const
{
// can access protected data of base class
return getBaseSalary() + CommissionEmployee::earnings();
}
void BasePlusCommissionEmployee::print() const
{
// can access protected data of base class
cout << "base-salaried ";
// invoke CommissionEmployee's print function
CommissionEmployee::print();
cout << "\nbase salary: " << getBaseSalary();
}
#include <iostream>
#include <iomanip>
using namespace std;
#include "BasePlusCommissionEmployee.h"
int main()
{
cout << fixed << setprecision(2);
{ // begin new scope
CommissionEmployee employee1("Bob", "Lewis", "111-11-1111", 1000, .01);
} // end scope
cout << endl;
BasePlusCommissionEmployee employee2("Peter", "Lee", "221-12-2222", 2000, .02, 222);
cout << endl;
BasePlusCommissionEmployee employee3("JJang-gu", "Shin", "331-13-3113", 3000, .03, 333);
cout << endl;
return 0;
}
CommissionEmployee constuctor:
commission employee: Bob Lewis
social security number: 111-11-1111
gross sales: 1000.00
commission rate: 0.01
CommissionEmployee desturctor:
commission employee: Bob Lewis
social security number: 111-11-1111
gross sales: 1000.00
commission rate: 0.01
CommissionEmployee constuctor:
commission employee: Peter Lee
social security number: 221-12-2222
gross sales: 2000.00
commission rate: 0.02
BasePlusCommissionEmployee constructor:
base-salaried commission employee: Peter Lee
social security number: 221-12-2222
gross sales: 2000.00
commission rate: 0.02
base salary: 222.00
CommissionEmployee constuctor:
commission employee: JJang-gu Shin
social security number: 331-13-3113
gross sales: 3000.00
commission rate: 0.03
BasePlusCommissionEmployee constructor:
base-salaried commission employee: JJang-gu Shin
social security number: 331-13-3113
gross sales: 3000.00
commission rate: 0.03
base salary: 333.00
BasePlusCommissionEmployee destructor:
base-salaried commission employee: JJang-gu Shin
social security number: 331-13-3113
gross sales: 3000.00
commission rate: 0.03
base salary: 333.00
CommissionEmployee desturctor:
commission employee: JJang-gu Shin
social security number: 331-13-3113
gross sales: 3000.00
commission rate: 0.03
BasePlusCommissionEmployee destructor:
base-salaried commission employee: Peter Lee
social security number: 221-12-2222
gross sales: 2000.00
commission rate: 0.02
base salary: 222.00
CommissionEmployee desturctor:
commission employee: Peter Lee
social security number: 221-12-2222
gross sales: 2000.00
commission rate: 0.02